//
//  WBNavigationController.swift
//  XtWeiBo
//
//  Created by 徐腾 on 2017/7/18.
//  Copyright © 2017年 徐腾. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 隐藏系统自带的导航栏
        navigationBar.isHidden = true
    }
    
    /// viewController 是在这里 push 的 ， 所以在这里设置它的左侧按钮为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 如果不是栈底控制器才需要隐藏， 根控制器不需要隐藏
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            var title = "返回"
            
            // 判断控制器的奇数
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? "返回"
            }
            
            if let vc = viewController as? WBBaseViewController {
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popVC), isBack: true)
                
            }
        }
        
        
        super.pushViewController(viewController, animated: true)
        
    }

}

extension WBNavigationController {
    
    func popVC() {
        popViewController(animated: true)
    }
    
}



